package com.crm.crm.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.crm.crm.domain.*;
import com.crm.crm.domain.dto.PageQueryParams;
import com.crm.crm.domain.vo.ClueProcessVo;
import com.crm.crm.domain.vo.PageCountResult;
import com.crm.crm.domain.vo.PageResult;
import com.crm.crm.domain.vo.ReportFunnelVo;
import com.crm.crm.mapper.*;
import com.crm.crm.service.ReportService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.util.List;
import java.util.ArrayList;
import java.util.List;
import java.util.List;
import java.util.List;

@Service
@Slf4j
public class ReportImpl implements ReportService {
    @Autowired
    private ClueMapper  clueMapper;
    @Autowired
    private ContractMapper contractMapper;
    @Autowired
    private UserMapper  userMapper;
    @Autowired
    private IndexMapper indexMapper;
    @Autowired
    private ChannelCountMapper channelCountMapper;



    @Override
    public ReportFunnelVo getVulnerabilityMap(LocalDate startDate, LocalDate endDate) {
        log.info("获取漏斗图数据，时间范围：{} 至 {}", startDate, endDate);
        ReportFunnelVo reportFunnelVo = new ReportFunnelVo();
        LambdaQueryWrapper<InsertRecord> wrapper = new LambdaQueryWrapper<>();
        wrapper.between(InsertRecord::getCreateTime, startDate, endDate);
        List<InsertRecord> list = indexMapper.selectList(wrapper);
        int clueNum = 0;
        int effectiveCluesNum = 0;
        int businessNum = 0;
        int contractNum = 0;
        for (InsertRecord insertRecord : list) {
            Integer clueCount = insertRecord.getClueCount();
            Integer clueChangeCount = insertRecord.getClueChangeCount();
            Integer businessCount = insertRecord.getBusinessCount();
            Integer businessChangeCount = insertRecord.getBusinessChangeCount();
            if (clueCount != null && clueChangeCount != null && businessCount != null && businessChangeCount != null) {
                clueNum += clueCount;
                effectiveCluesNum += clueChangeCount;
                businessNum += businessCount;
                contractNum += businessChangeCount;
            }
        }
        //线索总数
        reportFunnelVo.setCluesNums(clueNum);
        //有效线索数
        reportFunnelVo.setEffectiveCluesNums(effectiveCluesNum);
        //商机数
        reportFunnelVo.setBusinessNums(businessNum);
        //客户数
        reportFunnelVo.setContractNums(contractNum);
        return reportFunnelVo;
    }


    //这是渠道统计的分页查询
    @Override
    public PageCountResult<ChannelCount> activityStatisticsList(String beginCreateTime, String endCreateTime,
                                                                String code, String name, int pageNum, int pageSize) {
        log.info("开始分页查询活动统计列表，时间范围：{} 至 {}", beginCreateTime, endCreateTime);

        // 创建分页对象
        Page<ChannelCount> page = new Page<>(pageNum, pageSize);

        // 构建查询条件
        LambdaQueryWrapper<ChannelCount> wrapper = new LambdaQueryWrapper<>();
        if (beginCreateTime != null && !beginCreateTime.isEmpty()) {
            wrapper.ge(ChannelCount::getCreateTime, beginCreateTime);
        }
        if (endCreateTime != null && !endCreateTime.isEmpty()) {
            wrapper.le(ChannelCount::getCreateTime, endCreateTime);
        }
        if (code != null && !code.isEmpty()) {
            wrapper.like(ChannelCount::getCode, code);
        }
        if (name != null && !name.isEmpty()) {
            wrapper.like(ChannelCount::getName, name);
        }

        // 执行分页查询
        IPage<ChannelCount> resultPage = channelCountMapper.selectPage(page, wrapper);

        // 封装结果
        return PageCountResult.of(resultPage.getRecords(), (int) resultPage.getTotal());
    }
}
